﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;
using System.Text;

namespace RedemptoristRetreatCenter
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// On the click of the search button, it takes in the retreat title, start date and end date.
        /// A different search is done depending on whether or not all the fields are populated or not.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void searchButton_Click(object sender, EventArgs e)
        {
            ConversionUtility converter = new ConversionUtility();
            String sTitle = searchTitle.Text;
            String sStartDate = searchStartDate.Text;
            String sEndDate = searchEndDate.Text;
            DateTime sDate = DateTime.MinValue;
            DateTime eDate = DateTime.MaxValue;

            if (!sStartDate.Equals("") && !sEndDate.Equals(""))
            {
                sDate = converter.stringToDateTime(sStartDate);
                eDate = converter.stringToDateTime(sEndDate);
            }
            else if (((sStartDate.Equals("")) || (sEndDate.Equals(""))) && (sTitle.Equals("")))
            {
                if (sStartDate.Equals("") && sEndDate.Equals("") && sTitle.Equals(""))
                    statusMessage.Text = "Please enter a title or a start and end date";
                else
                    statusMessage.Text = "You must enter both a start and end date.";
            }
            
            if (!sTitle.Equals("") && sDate.Equals(DateTime.MinValue) && eDate.Equals(DateTime.MaxValue))
            {
                searchTheTitle(sender, e, sTitle);
            }
            else if(sTitle.Equals("") && !sDate.Equals(DateTime.MinValue) && !eDate.Equals(DateTime.MaxValue)){
                searchTheDateRange(sender, e, sDate, eDate);
            }
            else if (!sTitle.Equals("") && !sDate.Equals(DateTime.MinValue) && !eDate.Equals(DateTime.MaxValue))
            {
                searchTheTitleAndDateRange(sender, e, sTitle, sDate, eDate);        
            }
            
        }
        /// <summary>
        /// Searches the database to find all scheduled retreats similar to the searched title
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="title"></param>
        protected void searchTheTitle(object sender, EventArgs e, String title)
        {            
            StringBuilder idString = new StringBuilder();
            int id = -1;
            RetreatScheduleUtility rsu = new RetreatScheduleUtility();
            List<RetreatScheduleInfo> rsuList = rsu.searchByTitle(title);
            foreach (RetreatScheduleInfo rsi in rsuList)
            {
                id = rsi.scheduleId;
                idString.Append(rsi.scheduleId + ",");
            }
            if (id > -1)
            {
                idString.Remove(idString.Length - 1, 1);
                Response.Redirect("ViewScheduledRetreats.aspx" + "?ID=" + idString.ToString());
                base.OnLoad(e);
            }
            else
            {
                statusMessage.Text = "Retreat not found.";
            }
        }
        /// <summary>
        /// Searches the database for scheduled retreats within the dates specified.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        protected void searchTheDateRange(object sender, EventArgs e, DateTime start, DateTime end) 
        {
            StringBuilder idString = new StringBuilder();
            int id = -1;
            RetreatScheduleUtility rsu = new RetreatScheduleUtility();
            List<RetreatScheduleInfo> rsuList = rsu.searchByDateRange(start, end);

            foreach (RetreatScheduleInfo rsi in rsuList)
            {
                id = rsi.scheduleId;
                idString.Append(rsi.scheduleId + ",");
            }
            if (id > -1)
            {
                idString.Remove(idString.Length - 1, 1);
                Response.Redirect("ViewScheduledRetreats.aspx" + "?ID=" + idString.ToString());
                base.OnLoad(e);
            }
            else
            {
                statusMessage.Text = "Retreat not found.";
            }
        }
    
        /// <summary>
        /// Searches the database for retreats contain the title specified and within the daterange specified.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="title"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        protected void searchTheTitleAndDateRange(object sender, EventArgs e, String title, DateTime start, DateTime end)
        {
            StringBuilder idString = new StringBuilder();
            int id = -1;
            RetreatScheduleUtility rsu = new RetreatScheduleUtility();
            List<RetreatScheduleInfo> rsuList = rsu.searchByTitleAndDateRange(title, start, end);

            if ((searchStartDate.Text.Equals("")) || (searchEndDate.Text.Equals("")))
            {
                statusMessage.Text = "You must enter both a start and end date.";
            }
            else
            {
                foreach (RetreatScheduleInfo rsi in rsuList)
                {
                    id = rsi.scheduleId;
                    idString.Append(rsi.scheduleId + ",");
                }
                if (id > -1)
                {
                    idString.Remove(idString.Length - 1, 1);
                    Response.Redirect("ViewScheduledRetreats.aspx" + "?ID=" + idString.ToString());
                    base.OnLoad(e);
                }
                else
                {
                    statusMessage.Text = "Retreat not found.";
                }
            }
        }

    }
}